//
//  IndexHeaderController.m
//  yigou
//
//  Created by SunChao on 17/1/16.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "IndexHeaderController.h"
#import "SDCycleScrollView.h"
#import "CustomImgView.h"
#import "CJNaviView.h"
#import "IndexModel.h"
#import "CarouselfigureListModel.h"
#import "TribeDetailViewController.h"

#define imgWidth 90
#define imgHeight 90
#define imgSpace 7
typedef NS_OPTIONS(NSUInteger, SelectType) {
    SelectTypeArticle = 1,//文章
    SelectTypeTopic= 2//话题
};
@interface IndexHeaderController ()<SDCycleScrollViewDelegate>

@property (strong, nonatomic) SDCycleScrollView *cycleScrollView;

@property (weak, nonatomic)IBOutlet UIScrollView *midScrollview;

@property (weak, nonatomic) UIViewController *vc;

@property (weak, nonatomic) IBOutlet UILabel *zhiboLabel;
@property (weak, nonatomic) IBOutlet UILabel *paimaiLabel;
@property (weak, nonatomic) IBOutlet UILabel *zhongchouLabel;



@end

@implementation IndexHeaderController
-(NSMutableDictionary *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableDictionary new];
    }
    return _dataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadHeaderView) name:@"reloadHeaderView" object:nil];
    
    
    self.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, 556);
    self.headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 188);
    self.headerBottomView.frame = CGRectMake(0, 188, SCREEN_WIDTH, 176);
    self.midView.frame = CGRectMake(0, 364, SCREEN_WIDTH, 172);
}
- (void)reloadHeaderView{
    IndexModel *model = [[IndexModel alloc]initWithDictionary:self.dataSource error:nil];
    [self createHeaderViewWithArray:model.carouselfigureList];
    [self createFooterViewWithMenuArray:model.menuList];
    [self createMidViewWithTribeListArray:model.tribeList];
}

- (void)createHeaderViewWithArray:(NSArray *)array{
    
    if (self.cycleScrollView) {
        [self.cycleScrollView removeFromSuperview];
        self.cycleScrollView = nil;
    }
    
    CGFloat w = ScreenWidth;
    self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, w, 188) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
    
    self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    self.cycleScrollView.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    NSMutableArray *imgurls = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < array.count; i++) {
        NSDictionary *tmpDic = [array objectAtIndex:i];
        NSString *imgurl = [tmpDic objectForKey:@"pic_url"];
        [imgurls addObject:imgurl];
    }
    self.cycleScrollView.imageURLStringsGroup = imgurls;
    [self.headerView addSubview:self.cycleScrollView];
    
}
- (void)createFooterViewWithMenuArray:(NSArray *)menuArray{
   
    
//    self.zhiboLabel.text = @"";
//    self.zhongchouLabel.text = @"";
//    self.paimaiLabel.text = @"";
    
    for (NSDictionary *tmpDic in menuArray) {
         UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(MidImgViewTapAction:)];
        if ([[tmpDic objectForKey:@"key"]isEqualToString:@"ZHIBO"]) {
            [self.zhiboImg sd_setImageWithURL:[NSURL URLWithString:[tmpDic objectForKey:@"value"]] placeholderImage:[UIImage imageNamed:@""]];
            self.zhiboImg.userInteractionEnabled = YES;
            self.zhiboView.userInteractionEnabled = YES;
            [self.zhiboView addGestureRecognizer:tap];
        }else if ([[tmpDic objectForKey:@"key"]isEqualToString:@"PAIMAI"]){
             [self.paimaiImg sd_setImageWithURL:[NSURL URLWithString:[tmpDic objectForKey:@"value"]] placeholderImage:[UIImage imageNamed:@""]];
            [self.paimaiView addGestureRecognizer:tap];
            self.paimaiView.userInteractionEnabled = YES;
        }else if ([[tmpDic objectForKey:@"key"]isEqualToString:@"ZHONGCHOU"]){
            [self.zhongchouImg sd_setImageWithURL:[NSURL URLWithString:[tmpDic objectForKey:@"value"]] placeholderImage:[UIImage imageNamed:@""]];
            [self.zhongchouView addGestureRecognizer:tap];
        }
    }
}

- (void)createMidViewWithTribeListArray:(NSArray*)tribeArray{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, 100, 20)];
    label.font = [UIFont systemFontOfSize:15];
    label.text = @"热门部落";
    [self.midView addSubview:label];
    self.midScrollview.backgroundColor = [UIColor clearColor];
//    self.midScrollview.frame = CGRectMake(15, 52, ScreenWidth-30, imgHeight);
    self.midScrollview.showsHorizontalScrollIndicator = NO;
    NSUInteger numOfBlocks = tribeArray.count;
    self.midScrollview.contentSize = CGSizeMake(imgWidth * numOfBlocks+imgSpace*(numOfBlocks-1), 0);
    for(NSUInteger i = 0;  i< numOfBlocks; i++) {
        NSDictionary *dic = [tribeArray objectAtIndex:i];
        CustomImgView *block = [CustomImgView new];
        [block.img sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"tribe_logo"]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        block.title.text = [dic objectForKey:@"tribe_name"];
        block.title.font = [UIFont systemFontOfSize:13];
        block.frame = CGRectMake((imgWidth+imgSpace)*i, 10, imgWidth, 125);
        block.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImgAction:)];
        block.tag = i+100;
        [block addGestureRecognizer:tap];
        [self.midScrollview addSubview:block];
    }
}

- (void)MidImgViewTapAction:(UITapGestureRecognizer *)tap
{
    if ([tap.view isEqual:self.zhiboView]) {
        NSLog(@"直播点击了");
        [[NSNotificationCenter defaultCenter]postNotificationName:@"didSelectedZhiBo" object:nil];
        return;
    }
    if ([tap.view isEqual:self.paimaiView]) {
        NSLog(@"拍卖点击了");
        [[NSNotificationCenter defaultCenter]postNotificationName:@"didSelectedPaiMai" object:nil];
        return;
    }
    if ([tap.view isEqual:self.zhongchouView]) {
        NSLog(@"众筹点击了");
        [[NSNotificationCenter defaultCenter]postNotificationName:@"didSelectedZhongChou" object:nil];
        return;
    }
    
    
}

- (void)tapImgAction:(UITapGestureRecognizer *)tap{
    
     IndexModel *model = [[IndexModel alloc]initWithDictionary:self.dataSource error:nil];
    
    NSArray *array =  model.tribeList;
    
   NSDictionary *dic = [array objectAtIndex:tap.view.tag - 100];
    
    NSString *tribeID = [dic objectForKey:@"tribe_id"];
    if ([_delegate respondsToSelector:@selector(didClickHotTribeWithTribeID:)]) {
        
        [_delegate didClickHotTribeWithTribeID:tribeID];
    }
}

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    DYLog(@"---点击了第%ld张图片", (long)index);
    NSString *indexString = [NSString stringWithFormat:@"%ld",index];
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:indexString forKey:@"index"];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"didSelectScorllViewItemAtIndex" object:nil userInfo:dic];
}
@end
